www.gusucode.com > VC++ 模仿书本翻页效果的TXT电子书阅读器 > VC++ 模仿书本翻页效果的TXT电子书阅读器/code/TXTReader/FindDialog.cpp

    //Download by http://www.NewXing.com
// FindDialog.cpp : implementation file
//

#include "stdafx.h"
#include "txtreader.h"
#include "FindDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFindDialog dialog


CFindDialog::CFindDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CFindDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFindDialog)
	m_findString = _T("");
	m_findDirection = -1;
	//}}AFX_DATA_INIT
	m_bIsHere = 0;
	m_hBrush = ::CreateSolidBrush(RGB(126,250,243));
}


void CFindDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFindDialog)
	DDX_Control(pDX, IDCANCEL, m_cancel);
	DDX_Control(pDX, IDOK, m_findnext);
	DDX_Text(pDX, IDC_EDIT1, m_findString);
	DDV_MaxChars(pDX, m_findString, 20);
	DDX_Radio(pDX, IDC_DOWN, m_findDirection);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFindDialog, CDialog)
	//{{AFX_MSG_MAP(CFindDialog)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_UP, OnUp)
	ON_BN_CLICKED(IDC_DOWN, OnDown)
	ON_EN_UPDATE(IDC_EDIT1, OnUpdateEdit1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindDialog message handlers

void CFindDialog::OnFindnext() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_bIsHere==1)
	{
		m_cur = m_nCurSave;
		m_bIsHere = 0;
	}

	if(strcmp(m_findString.LockBuffer(),"")==0)
	{
		m_findString.UnlockBuffer();
		return;
	}
	CFile find;
	if(!find.Open(m_findFilePathname,CFile::modeRead | CFile::shareDenyNone))
	{
		MessageBox("打开文件失败!");
		return;
	}
	int lengthofile = find.GetLength();
	char temp[READ_ONE_OF_BYTES+1] = {0};
	CString findWhat;
	CString inString;
	UINT numofbytesToread = READ_ONE_OF_BYTES;
	int curP = 1;
	if(m_findDirection == 1)//向前找
	{
		m_cur += 20;
		findWhat = m_findString;
		findWhat.MakeReverse();//得到要查找的子串
		
		while(curP>0)
		{
			curP = m_cur - READ_ONE_OF_BYTES;
			if(curP<0)//如果curP的值小于零,要修改其值,和最后一次读取数据的值
			{
				curP=0;
				numofbytesToread = m_cur-1; 
			}
			find.Seek(curP,CFile::begin);
			memset(temp,0,sizeof(temp));
			find.Read(temp,numofbytesToread);

			inString = temp;
			inString.MakeReverse();//取得父串
			int off = inString.Find(findWhat);
			if(off==-1)//在该父串中没有找到的情况,循环继续
			{
				m_cur = curP + 20;
				continue;
			}
			//在父串中找到子串
			
			m_cur = m_cur - off - findWhat.GetLength();
			m_nCurSave = m_cur;
			UINT page = m_pTxt->m_curPageL+1,line;
			while(page>0)//根据找到的位置确定其在书的页和行
			{
				PageNode *pPage = m_pTxt->GetPage(page);
				if(!pPage)
				{
					page--;
					continue;
				}
				if(m_cur>=pPage->line0[0] && m_cur<pPage->line0[LINE_COUNT-1]\
					+ pPage->line1[LINE_COUNT-1])//找到所属页
				{
					for(int i=0;i<LINE_COUNT;i++)
					{
						if(m_cur>=pPage->line0[i] && m_cur<pPage->line0[i] +pPage->line1[i])//找到所属的行
						{
							line = i+1;
							m_cur -= 20;
							find.Close();
							//翻到找到的页面并在找到的地方作记号
							m_pTxt->m_pWnd->m_Page.SetFindMark(page,line);
							return;
						}
					}
				}
				page--;
			}
		}
		m_cur = m_nCurSave;
		MessageBox("向前没有找到匹配的字符串!",NULL,MB_OK | MB_ICONWARNING);
		find.Close();
		return;
	}
	if(m_findDirection == 0)//向后找
	{
		m_cur -= 20;
		if(m_cur<0)m_cur=0;
		findWhat = m_findString;
		while(curP<lengthofile)
		{
			curP = m_cur + READ_ONE_OF_BYTES;
			if(curP>lengthofile)
			{
				curP = lengthofile;
				numofbytesToread = lengthofile - m_cur;
			}
			find.Seek(m_cur,CFile::begin);
			memset(temp,0,sizeof(temp));
			find.Read(temp,numofbytesToread);
			
			inString = temp;//取得父串
			int off = inString.Find(findWhat);
			if(off==-1)
			{
				m_cur = curP - 20;
				continue;
			}
			//在父串中找到子串
			m_cur = m_cur + off;
			m_nCurSave = m_cur;
			UINT page = m_pTxt->m_curPageL,line;
			while(page<=m_pTxt->m_totalPage)//根据找到的位置确定其在书的页和行
			{
				PageNode *pPage = m_pTxt->GetPage(page);
				if(!pPage)
				{
					page++;
					continue;
				}
				if(m_cur>=pPage->line0[0] && m_cur<pPage->line0[LINE_COUNT-1]\
					+ pPage->line1[LINE_COUNT-1])//找到所属的页
				{
					for(int i=0;i<LINE_COUNT;i++)
					{
						if(m_cur>=pPage->line0[i] && m_cur<pPage->line0[i] + pPage->line1[i])//找到所属的行
						{
							line = i+1;
							m_cur = m_cur + findWhat.GetLength()+20;
							find.Close();
							//翻到找到的页面并在找到的地方作记号
							m_pTxt->m_pWnd->m_Page.SetFindMark(page,line);
							return;
						}
					}
				}
				page++;
			}
		}
		m_cur = m_nCurSave;
		MessageBox("向后没有找到匹配的字符串!",NULL,MB_OK | MB_ICONWARNING);
		find.Close();
		return;
	}
}

BOOL CFindDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	((CButton*)GetDlgItem(IDC_DOWN))->SetCheck(1);
	GetDlgItem(IDC_EDIT1)->SetFocus();
	return TRUE;  // return TRUE unless you set the focus to a control
}


HBRUSH CFindDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	pDC->SetTextColor(RGB(255,0,0));
	// TODO: Change any attributes of the DC here
	if(pWnd == this)
	{
		return m_hBrush;
	}
	if(nCtlColor == CTLCOLOR_STATIC)
	{
		pDC->SetBkMode(TRANSPARENT);
		//HBRUSH brush =(HBRUSH)::GetStockObject(NULL_BRUSH);
		return m_hBrush;
	}
	if(nCtlColor == CTLCOLOR_BTN)
	{
		return m_hBrush;
	}
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CFindDialog::OnOK() 
{
	// TODO: Add extra validation here
	OnFindnext();
	//CDialog::OnOK();
}

void CFindDialog::OnUp() 
{
	// TODO: Add your control notification handler code here
	m_bIsHere = 1;
}

void CFindDialog::OnDown() 
{
	// TODO: Add your control notification handler code here
	m_bIsHere = 1;
}

void CFindDialog::OnUpdateEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	m_bIsHere = 1;
	// TODO: Add your control notification handler code here
	
}